<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change" :is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
				<swiper-item v-for="i in 5" :key='i' class="swiper-item">
					<scroll-view v-if="orderList[i].length>0" scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view>
								<view class="order" v-for="(res, index) in orderList[i]" v-if="res.orderState!=10" :key="res.id">
									<view class="top">
										<view class="left">
											<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
											<view class="store">{{ res.hotel?res.hotel.hotelName:'已删除' }}</view>
											<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
										</view>
										<view class="right">{{ orderTypeList[res.orderState-1] }}</view>
									</view>
									<view class="text-gray margin-top">
										<view class="flex">
											<view>预订时间:</view>
											<view>{{res.beginTime.substr(0,10)}}至{{res.endTime.substr(0,10)}}</view>
										</view>
										<view class="flex">
											<view>订单号:</view>
											<view>{{res.orderNo}}</view>
										</view>
										<view class="flex">
											<view>房型:</view>
											<view>{{res.hotelRoomType?res.hotelRoomType.typeName:"已删除"}}</view>
										</view>
										<view class="flex">
											<view>押金:</view>
											<view>{{res.depositMoney}}</view>
										</view>
										<!-- <view class="flex">
											<view>总价:</view>
											<template v-if="res.orderInfo.payType==2">
												<view>{{res.orderInfo.}}</view>
											</template>
											<template v-else>
												<view>{{}}</view>
											</template>
										</view> -->
									</view>
									<view class="bottom">
										<view class="more">
											<!-- <u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon> -->
										</view>
										<view>
											<!-- <view class="cu-btn sm round line-gray margin-left">发票申请</view> -->
											<view v-if="res.orderState==8" @tap='lookDetail' :data-obj='res' class="cu-btn sm round line-orange margin-left">查看明细</view>
											<view v-if="res.orderState==1"
											class="cu-btn sm round line-orange margin-left" 
											:data-id='res.id'
											@tap='cancalOrder'
											>取消订单</view>
											<view v-if="res.orderState==6||res.orderState==8||res.orderState==9" 
											class="cu-btn sm round line-orange margin-left" 
											:data-id='res.id'
											@tap='delOrder'
											>删除订单</view>
											<view v-if="res.orderState==1" @click='payOrder' :data-id='res.id' class="cu-btn sm round line-orange margin-left">立即支付</view>
											<view v-else-if="res.orderState==4" class="cu-btn sm round line-orange margin-left">立即离店</view>
										</view>
									</view>
								</view>
								<u-loadmore :status="loadStatus[i]" bgColor="#f2f2f2"></u-loadmore>
							</view>
						</view>
					</scroll-view>
					<view v-else style="margin-top: 200rpx;">
						<u-empty text="订单为空" mode="order"></u-empty>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="cu-modal" :class="modalName=='lookPrice'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">订单明细</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="bg-white">
					<form>
						<view class="cu-form-group">
							<view class="title">房间费用</view>
							<view>{{orderDetail.roomMoney}}</view>
						</view>
						<view class="cu-form-group">
							<view class="title">房间内消费</view>
							<view>{{orderDetail.consumptionMoney}}</view>
						</view>
						<view class="cu-form-group">
							<view class="title">其他消费</view>
							<view>{{orderDetail.otherMoney}}</view>
						</view>
						<view class="cu-form-group">
							<view class="title">合计</view>
							<view>{{orderDetail.roomMoney+orderDetail.consumptionMoney+orderDetail.otherMoney}}</view>
						</view>
					</form>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {orderList,continuePayOrder,checkPayStatus,
	deleteOrder,cancelUnPayOrder} from '../../../api/api.js'
export default {
	data() {
		return {
			orderList: [[], [], [], [],[]],
			dataList: [],
			list: [
				{
					name: '全部'
				},
				{
					name: '待付款'
				},
				{
					name: '已接单'
				},
				{
					name: '已入住'
				},
				{
					name: '已完成'
				}
			],
			orderStatus:[null,1,3,4,8],
			current: 0,
			swiperCurrent: 0,
			tabsHeight: 0,
			dx: 0,
			page:1,
			size:5,
			loadStatus: ['loadmore','loadmore','loadmore','loadmore','loadmore'],
			orderTypeList: ["未支付",'已支付','已接单','已入住','离店','已取消','不接单','离店完成','取消确认','删除'],
			modalName:null,
			orderDetail:{}
		};
	},
	onLoad() {
		this.getOrderList(0);
	},
	computed: {
		
	},
	methods: {
		lookDetail(res){
			this.modalName = 'lookPrice';
			this.orderDetail = res.currentTarget.dataset.obj;
			console.log(this.orderDetail)
		},
		hideModal(){
			this.modalName = '';
		},
		cancalOrder(row){
			let id = row.currentTarget.dataset.id;
			cancelUnPayOrder({
				orderInfoId:id
			}).then(res=>{
				if(res.code==200){
					uni.showToast({
						title:"取消成功",
						icon:"success"
					})
					this.getOrderList(this.current);
				}
			})
		},
		delOrder(row){
			let id = row.currentTarget.dataset.id;
			deleteOrder({
				orderInfoId:id
			}).then(res=>{
				if(res.code==200){
					uni.showToast({
						title:"删除成功",
						icon:"success"
					})
					this.getOrderList(this.current)
				}
			})
		},
		payOrder(row){
			let id = row.currentTarget.dataset.id;
			continuePayOrder({
				orderInfoId:id
			}).then(res=>{
				if(res.code==200){
					let obj = JSON.parse(res.data.prePayInfo);
					uni.requestPayment({
						provider:"wxpay",
						timeStamp:obj.timeStamp,
						nonceStr:obj.nonceStr,
						'package':obj.packageValue,
						paySign:obj.paySign,
						signType:obj.signType,
						success(e) {
							if(e.errMsg=='requestPayment:ok'){
								uni.showLoading({
									title:"正在下单请稍等"
								})
								checkPayStatus({
									orderInfoId:res.data.orderInfoId,
									outTradeNo:res.data.outTradeNo
								}).then(payRes=>{
									uni.hideLoading();
									if(payRes.code==200){
										uni.switchTab({
											url:"/pages/tabbar/cool/cool"
										})
									}else{
										uni.redirectTo({
											url:"/pages/mine/orderList/orderList"
										})
									}
								})
							}
						},
					})
				}
			})
			
		},
		reachBottom() {
			// 此tab为空数据
			this.loadStatus.splice(this.current,1,"loading");
			this.page++;
			this.addOrderList(this.current);
		},
		addOrderList(idx){
			orderList({
				orderState:this.orderStatus[idx],
				page:this.page,
				size:this.size
			}).then(res=>{
				if(res.code==200){
					if(res.data.list.length<=0){
						uni.showToast({
							icon:"none",
							title:"没有更多了"
						})
					}
					this.$set(this.orderList,idx,this.orderList[idx].concat(res.data.list));
				}
				this.loadStatus.splice(this.current,1,"loadmore")
			})
		},
		// 页面数据
		getOrderList(idx) {
			orderList({
				orderState:this.orderStatus[idx],
				page:this.page,
				size:this.size
			}).then(res=>{
				if(res.code==200){
					if(res.data.list.length<=0){
						uni.showToast({
							icon:"none",
							title:"没有更多了"
						})
					}
					this.$set(this.orderList,idx,res.data.list);
				}
				this.loadStatus.splice(this.current,1,"loadmore")
			})
		},
		// tab栏切换
		change(index) {
			this.swiperCurrent = index;
			this.page=1;
			this.getOrderList(index);
		},
		transition({ detail: { dx } }) {
			this.page=1;
			this.$refs.tabs.setDx(dx);
		},
		animationfinish({ detail: { current } }) {
			this.$refs.tabs.setFinishCurrent(current);
			this.swiperCurrent = current;
			this.current = current;
		}
	}
};
</script>

<style lang="scss" scoped>
.order {
	width: 710rpx;
	background-color: #ffffff;
	margin: 20rpx auto;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;
	.top {
		display: flex;
		justify-content: space-between;
		.left {
			display: flex;
			align-items: center;
			.store {
				margin: 0 10rpx;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
		.right {
			color: $u-type-warning-dark;
		}
	}
	.item {
		display: flex;
		margin: 20rpx 0 0;
		.left {
			margin-right: 20rpx;
			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}
		.content {
			.title {
				font-size: 28rpx;
				line-height: 50rpx;
			}
			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}
			.delivery-time {
				color: #e5d001;
				font-size: 24rpx;
			}
		}
		.right {
			margin-left: 10rpx;
			padding-top: 20rpx;
			text-align: right;
			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}
			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}
	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;
		.total-price {
			font-size: 32rpx;
		}
	}
	.bottom {
		display: flex;
		margin-top: 40rpx;
		padding: 0 10rpx;
		justify-content: space-between;
		align-items: center;
	}
	.flex{
		&>view:first-child{
			width: 200rpx;
			line-height: 1.5rem;
		}
	}
}
.centre {
	text-align: center;
	margin: 200rpx auto;
	font-size: 32rpx;
	image {
		width: 164rpx;
		height: 164rpx;
		border-radius: 50%;
		margin-bottom: 20rpx;
	}
	.tips {
		font-size: 24rpx;
		color: #999999;
		margin-top: 20rpx;
	}
	.btn {
		margin: 80rpx auto;
		width: 200rpx;
		border-radius: 32rpx;
		line-height: 64rpx;
		color: #ffffff;
		font-size: 26rpx;
		background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
	}
}
.wrap {
	display: flex;
	flex-direction: column;
	height: calc(100vh - var(--window-top));
	width: 100%;
}
.swiper-box {
	flex: 1;
}
.swiper-item {
	height: 100%;
}
</style>
